using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class DonGiaNguyenVatLieuDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveDonGiaNguyenVatLieu(DonGiaNguyenVatLieu dongianguyenvatlieu)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddDonGiaNguyenVatLieu", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddDonGiaNguyenVatLieu";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, dongianguyenvatlieu.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdHangHoa", DbType.Int32, ParameterDirection.Input, dongianguyenvatlieu.IdHangHoa);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdNhaCungCap", DbType.Int32, ParameterDirection.Input, dongianguyenvatlieu.IdNhaCungCap);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NgayThang", DbType.DateTime, ParameterDirection.Input, dongianguyenvatlieu.NgayThang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonGia", DbType.Int64, ParameterDirection.Input, dongianguyenvatlieu.DonGia);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, dongianguyenvatlieu.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all DonGiaNguyenVatLieu in DB
		public ResultObj GetAllDonGiaNguyenVatLieu( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<DonGiaNguyenVatLieu> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllDonGiaNguyenVatLieu", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<DonGiaNguyenVatLieu>();
			DonGiaNguyenVatLieu item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new DonGiaNguyenVatLieu();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdHangHoa = Convert.ToInt32(reader["IdHangHoa"]);
			item.IdNhaCungCap = Convert.ToInt32(reader["IdNhaCungCap"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all DonGiaNguyenVatLieu in DB with condition
		public ResultObj GetAllDonGiaNguyenVatLieuByID(DonGiaNguyenVatLieu dongianguyenvatlieu)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<DonGiaNguyenVatLieu> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllDonGiaNguyenVatLieuByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, dongianguyenvatlieu.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<DonGiaNguyenVatLieu>();
			DonGiaNguyenVatLieu item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new DonGiaNguyenVatLieu();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdHangHoa = Convert.ToInt32(reader["IdHangHoa"]);
			item.IdNhaCungCap = Convert.ToInt32(reader["IdNhaCungCap"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a DonGiaNguyenVatLieu out of DB with condition
		public ResultObj DeleteDonGiaNguyenVatLieu(DonGiaNguyenVatLieu dongianguyenvatlieu)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteDonGiaNguyenVatLieu", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteDonGiaNguyenVatLieu";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, dongianguyenvatlieu.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of HangHoa from DB with condition
		public ResultObj GetHangHoa(int IdHangHoa)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<HangHoa> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetHangHoa", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdHangHoa);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<HangHoa>();
			HangHoa item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new HangHoa();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.Ten = Convert.ToString(reader["Ten"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ChuoiTimKiem = Convert.ToString(reader["ChuoiTimKiem"]);
			item.NoiLuuTru = Convert.ToString(reader["NoiLuuTru"]);
			item.IdKhayDung = Convert.ToInt32(reader["IdKhayDung"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get info of NhaCungCap from DB with condition
		public ResultObj GetNhaCungCap(int IdNhaCungCap)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<NhaCungCap> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetNhaCungCap", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdNhaCungCap);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<NhaCungCap>();
			NhaCungCap item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new NhaCungCap();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.TenNhaCungCap = Convert.ToString(reader["TenNhaCungCap"]);
			item.DiaChi = Convert.ToString(reader["DiaChi"]);
			item.DienThoai = Convert.ToString(reader["DienThoai"]);
			item.MaSoThue = Convert.ToString(reader["MaSoThue"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}

        // Get DonGiaNguyenVatLieu
        public ResultObj GetDonGiaNguyenVatLieu(DonGiaNguyenVatLieu dongianguyenvatlieu)
        {
            ResultObj result = new ResultObj();

            result.Result = null;
            List<DonGiaNguyenVatLieu> lst = null;

            SqlCommand cmd = CreateSqlCommand("spGetDonGiaByNhaCungCap", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, dongianguyenvatlieu.Id);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IdHangHoa", DbType.Int32, ParameterDirection.Input, dongianguyenvatlieu.IdHangHoa);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IdNhaCungCap", DbType.Int32, ParameterDirection.Input, dongianguyenvatlieu.IdNhaCungCap);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<DonGiaNguyenVatLieu>();
                DonGiaNguyenVatLieu item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new DonGiaNguyenVatLieu();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.IdHangHoa = Convert.ToInt32(reader["IdHangHoa"]);
                    item.DonGia = Convert.ToInt64(reader["DonGia"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);

                    HangHoa hanghoa = new HangHoa();
                    hanghoa.Ten = Convert.ToString(reader["Ten"]);
                    item.hanghoa = hanghoa;
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }
	}
}
